package model

import (
	"global/structs"
	"fmt"
)

const USER_FEE_NO = 1
const USER_FEE_YES = 0

type userModel struct {}

var UserModel = new(userModel)

// 获取免手续费的用户
func (userModel) GetNoFeeUIds() ([]int, error) {
	users := make([]structs.User, 0)
	err := MainDB.Where("no_fee=?", USER_FEE_NO).Cols("uid", "status").Find(&users)
	if err !=nil {
		return nil, err
	}
	ids := make([]int, 0)
	for _, u := range users {
		ids = append(ids, u.Uid)
	}
	return ids, nil
}

func (userModel) GetUserByUID(uid int) (*structs.User, error) {
	user := new(structs.User)
	has, err := MainDB.Where("uid=?", uid).Get(user)
	if err != nil {
		return nil, err
	}
	if has {
		return user, nil
	}
	return nil, fmt.Errorf("user not found")
}